/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [GobangThread.java]
 * Creation Date        [22-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_03_game.gobang.version_6;

import javax.swing.JOptionPane;

/**
 * 五子棋线程类
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class GobangThread implements Runnable {

	private GobangFrame frame = null;

	public GobangThread(GobangFrame frame) {
		this.frame = frame;
	}

	@Override
	public void run() {
		if (frame.maxTime > 0) {
			while (true) {
				if (frame.isBlack && frame.isContinue) {// 黑方下棋
					--frame.blackTime;
					if (frame.blackTime == 0) {
						JOptionPane.showMessageDialog(frame, "黑方超时, 游戏结束");
						frame.isContinue = false;
					}
				} else { // 白方下棋
					--frame.whiteTime;
					if (frame.whiteTime == 0) {
						JOptionPane.showMessageDialog(frame, "白方超时, 游戏结束");
						frame.isContinue = false;
					}
				}
				showTimeMessage();
				if (frame.blackTime >= 0 && frame.whiteTime >= 0
						&& frame.isContinue) {
					frame.repaint();
				}

				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 显示双方时间
	 */
	private void showTimeMessage() {
		frame.blackMessage = frame.blackTime / 3600 + ":"
				+ (frame.blackTime / 60 - frame.blackTime / 3600 * 60) + ":"
				+ (frame.blackTime - frame.blackTime / 60 * 60);
		frame.whiteMessage = frame.whiteTime / 3600 + ":"
				+ (frame.whiteTime / 60 - frame.whiteTime / 3600 * 60) + ":"
				+ (frame.whiteTime - frame.whiteTime / 60 * 60);
	}
}